/**
 * 部门树形下拉菜单
 * Created by wuwt on 2015/7/28.
 * 使用说明:
   js: $(function(){
            setDepartmentTreeMenu("department");
       })
   html:<div class="form-group">
            <label class="col-sm-2 control-label">部门：</label>
            <div class="col-sm-4">
                <input class="form-control" type="text" id="department_name" name="departmentName" onclick="showDepartmentTreeMenu(this.id);" readonly>
                <input type="hidden" id="department_id" name="departmentId">
            </div>
        </div>
 *
 */
var getDepartmentList_url = "/osm/controller/department/getDepartmentList";
var treeMenuDiv = "departmentTreeMenu_div";
var treeMenu = "departmentTreeMenu";
$(function(){
    var div = '<div id="departmentTreeMenu_div" class="ztreemenu" style="display:none; position: absolute;z-index: 1888;">';
    div+='<ul id="departmentTreeMenu" class="ztree" style="margin-top:0;height:300px;overflow: auto;"></ul></div>';
    $("#mainScroll").after(div);
})
//加载部门树形下拉菜单
function setDepartmentTreeMenu(input){
    var setting = {
        view: {
            dblClickExpand:false
        },
        data:{
            simpleData:{
                enable: true
            }
        },
        callback: {
            onClick: getMethod(input)
        }
    };
    //加载部门树形节点
    $.post(getDepartmentList_url,function(data){
        var departmentList = data.departmentList;
        var companyName = data.companyName;
        var zNodes = "[{ id:'0', pId:'0', name:companyName, path:'', open:true, noRemoveBtn: true, noRenameBtn: true}";
        $.each(departmentList,function(i,dept){
            var id = dept.departmentId;
            var pId = dept.parentId==''?'0':dept.parentId;
            var name = dept.departmentName;
            var pName = dept.parentName==''?companyName:dept.parentName;
            var deptNum = dept.departmentNum;
            var orderNum = dept.orderNum;
            var path = dept.path;
            zNodes+=",{id: '"+id+"', pId: '"+pId+"', name: '"+name+"', pName: '"+pName+"', deptNum: '"+deptNum+"', orderNum: "+orderNum+", path: '"+path+"', open: true}";
        });
        zNodes+="]";
        $.fn.zTree.init($("#"+treeMenu), setting, eval(zNodes));
    })
}
function getMethod(input) {
    return function(e, treeId, treeNode){
        var zTree = $.fn.zTree.getZTreeObj(treeId);
        var nodes = zTree.getSelectedNodes();
        var nodeId = nodes[0].id;
        var nodeName = nodes[0].name;
        $("#"+input+"_id").val(nodes[0].id);
        $("#"+input+"_name").val(nodes[0].name);
        hideMenu();
    };
}
function showDepartmentTreeMenu(inputId){
    var input = $("#"+inputId);
    var inputOffset = $(input).offset();
    $("#"+treeMenuDiv).css({left:inputOffset.left + "px", top:inputOffset.top + input.outerHeight() + "px",width:input.width()+26+"px"}).slideDown("fast");
    $("body").bind("mousedown", onBodyDown);
}
function hideMenu(){
    $("#"+treeMenuDiv).fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);
}
function onBodyDown(event){
    if (!(event.target.id == "menuBtn" || event.target.id == treeMenuDiv || $(event.target).parents("#"+treeMenuDiv).length>0)) {
        hideMenu();
    }
}